import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  { path: '/home', component: () => import('../views/HomePage.vue') ,redirect: '/home/productlist',
    children: [
        { path: 'productlist', component: () => import('../views/ProductsList.vue')}]
  },
  { path: '/product/:id', component: () => import('../views/ProductDetail.vue'),name:'ProductDetail'},
  { path: '/points-detail', component: () => import('../views/PointsDetail.vue'), name: 'PointsDetail' },
  { path: '/points-order', component: () => import('../views/PointsOrder.vue'), name: 'PointsOrder' },
  { path: '/order-detail/:id', component: () => import('../views/OrderDetail.vue'), name: 'OrderDetail' }
]
//创建路由守卫

const router = createRouter({
  history: createWebHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  //判断是否登录
  if (to.path === '/login' || to.path === '/register') {
    next()
  } else {
    const userId = localStorage.getItem('userId')
    console.log(userId);
    
    if (userId) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router